package com.company.behavioral.memento_pattern.client;

import com.company.behavioral.memento_pattern.caretaker.MementoCaretakers;
import com.company.behavioral.memento_pattern.originator.Chessman;

/**
 * 实现多次撤销
 * @ClassName Client
 * @Author tiezheng.han
 * @Date 2020/9/4 18:18
 **/
public class Client1 {
    // 定义一个索引来记录当前状态所在的位置
    private static int index = -1;
    private static MementoCaretakers mc = new MementoCaretakers();
    public static void main(String[] args) {
        Chessman chess = new Chessman("车",1,1);
        play(chess);
        chess.setY(4);
        play(chess);
        chess.setX(5);
        play(chess);
        undo(chess,index);
        undo(chess,index);
        redo(chess,index);
        redo(chess,index);
    }

    /**
     * 下棋
     * @param chess
     */
    public static void play(Chessman chess){
        mc.setMemento(chess.save());
        index++;
        System.out.println("棋子" + chess.getLable() + "当前位置为：" + "第" + chess.getX() +
                "行" + "第" + chess.getY() + "列。");
    }

    /**
     * 悔棋
     * @param chess
     * @param i
     */
    public static void undo(Chessman chess,int i){
        System.out.println("*************悔棋**************");
        index--;
        //撤销到上一个备忘录
        chess.restore(mc.getMemento(i-1));
        System.out.println("棋子" + chess.getLable() + "当前位置为：" + "第" + chess.getX() +
                "行" + "第" + chess.getY() + "列。");
    }

    /**
     * 撤销悔棋
     * @param chess
     * @param i
     */
    public static void redo(Chessman chess,int i){
        System.out.println("*************撤销悔棋**************");
        index++;
        //撤销到下一个备忘录
        chess.restore(mc.getMemento(i+1));
        System.out.println("棋子" + chess.getLable() + "当前位置为：" + "第" + chess.getX() +
                "行" + "第" + chess.getY() + "列。");
    }
}
